package top.zywork.strategy.order;

import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import top.zywork.dao.AccountDetailDAO;
import top.zywork.dao.PlatformAccountDetailDAO;
import top.zywork.dos.AccountDetailDO;
import top.zywork.dos.PlatformAccountDetailDO;
import top.zywork.enums.*;
import top.zywork.service.SysConfigService;
import top.zywork.weixin.*;

import java.math.BigDecimal;

/**
 * @author Animo
 * @Date 2020/10/31
 * @Blog http://www.ljyanimo.com
 */
@Service
public class BaseOrder {

    private AccountDetailDAO accountDetailDAO;

    private PlatformAccountDetailDAO platformAccountDetailDAO;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 保存用户账目
     * @param userId 用户id
     * @param transactionNo 流水号或者订单号
     * @param type 支出或者收入
     * @param amount 金额
     * @param subType 来源类型
     * @param orderIds 备注订单号等
     */
    public void saveAccountDetail(Long userId, String transactionNo, Integer type, BigDecimal amount, String subType, String orderIds) {
        AccountDetailDO accountDetailDO = AccountDetailDO.builder()
                .userId(userId)
                .transactionNo(transactionNo)
                .amount(amount)
                .type(type.byteValue())
                .subType(subType)
                .payType(PaymentTypeEnum.WEIXIN_PAY.getValue().byteValue())
                .accountStatus(AccountStatusEnum.WAIT_CONFIRM.getValue().byteValue())
                .remark(subType + "支出，订单信息：" + orderIds).build();
        accountDetailDAO.save(accountDetailDO);
    }

    public WXPayConfig getXcxConfig(){
        return sysConfigService.getByName(SysConfigEnum.WX_PAY_CONFIG.getValue(), WXPayConfig.class);
    }

    /**
     * 保存平台账目
     * @param userId
     * @param transactionNo
     * @param amount
     * @param orderIds
     */
    public void savePlatformAccountDetail(Long userId, String transactionNo, String subType, BigDecimal amount, String orderIds) {
        PlatformAccountDetailDO platformAccountDetailDO = PlatformAccountDetailDO.builder()
                .userId(userId)
                .transactionNo(transactionNo)
                .amount(amount)
                .type(IncomeOutgoingTypeEnum.INCOME.getValue().byteValue())
                .subType(subType)
                .payType(PaymentTypeEnum.WEIXIN_PAY.getValue().byteValue())
                .accountStatus(AccountStatusEnum.WAIT_CONFIRM.getValue().byteValue())
                .remark(subType + "收入，订单信息：" + orderIds).build();
        platformAccountDetailDAO.save(platformAccountDetailDO);
    }

    public void sendTemplateMessage(XcxTemplateMsg xcxTemplateMsg){
        WeixinXcxConfig weixinXcxConfig = sysConfigService.getByName(SysConfigEnum.WEIXIN_XCX_CONFIG.getValue(), WeixinXcxConfig.class);
        String accessToken = WeixinUtils.getAccessToken(redisTemplate, weixinXcxConfig.getAppId(), weixinXcxConfig.getAppSecret(), ClientAppEnum.WEIXIN_XCX);
        boolean b = WeixinMsgUtils.sendXcxTemplateMsg(accessToken, xcxTemplateMsg);
        System.out.println("发送模板消息状态：" + b);
    }

    @Autowired
    public void setAccountDetailDAO(AccountDetailDAO accountDetailDAO) {
        this.accountDetailDAO = accountDetailDAO;
    }

    @Autowired
    public void setPlatformAccountDetailDAO(PlatformAccountDetailDAO platformAccountDetailDAO) {
        this.platformAccountDetailDAO = platformAccountDetailDAO;
    }

}
